/***************************************************************************
 *   Copyright (C) 2007 by Ivan Vasi?                                      *
 *   ivasic@gmail.com                                                      *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "userpropertiesdialog.h"

#include "modeluser.h"
#include "globals.h"
#include "trackerdb.h"
#include "modeltorrent.h"
#include "torrentpropertiesdialog.h"
#include "functions.h"

#include "exceptions.h"
#include "exceptionhandler.h"

#include <QtGui>

UserPropertiesDialog::UserPropertiesDialog(ModelUser* user, QWidget* parent, Qt::WFlags fl)
	: QDialog(parent, fl), m_user(user)
{
	setupUi(this);
	
	m_incompleteModel = Globals::instance().db()->getTorrentListForUser(m_user->username(), FALSE);	
	m_completeModel = Globals::instance().db()->getTorrentListForUser(m_user->username(), TRUE);
	
	fillForm();
	
	
	connect(btnResetPasskey, SIGNAL(clicked()), this, SLOT(resetPasskey()));
	connect(btnEdit, SIGNAL(clicked()), this, SLOT(changeInfo()));
	connect(listIncomplete, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(listIncompleteDoubleClicked(QModelIndex)));
	connect(listComplete, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(listCompleteDoubleClicked(QModelIndex)));
}

UserPropertiesDialog::~UserPropertiesDialog()
{
	delete m_incompleteModel;
	delete m_completeModel;
}

/*$SPECIALIZATION$*/

void UserPropertiesDialog::fillForm()
{
	lblName->setText(m_user->name() + " (" + m_user->username() + ")");
	lblJoined->setText(m_user->joined().toString("dd/mm/yy"));
	
	lblDownloaded->setText(bytesToString(m_user->downloaded()));
	lblUploaded->setText(bytesToString(m_user->uploaded()));
	lblRatio->setText(QString("%1").arg((double) m_user->uploaded()/m_user->downloaded(),0,'g',2));
	
	lblIncomplete->setText(QString("%1").arg(m_incompleteModel->rowCount()));
	lblComplete->setText(QString("%1").arg(m_completeModel->rowCount()));
	
	txtEmail->setText(m_user->email());
	txtPassword->setText(m_user->password());	
	
	listIncomplete->setModel(m_incompleteModel);
	listComplete->setModel(m_completeModel);
	
	txtAnnounceURL->setText("http://127.0.0.1:" + Globals::instance().settings()->value("server_port", 23982).toString() + "/" + m_user->passkey() + "/announce");
}

void UserPropertiesDialog::resetPasskey()
{
	try
	{
		Globals::instance().db()->resetPasskey(m_user->username(), m_user->password());
		
		QMessageBox::information(this, tr("Success"), tr("Passkey reset."));
	}
	catch (Exception* ex)
	{
		ExceptionHandler::instance().protocolException(this->thread(), ex, TRUE);
	}
}

void UserPropertiesDialog::changeInfo()
{
	try
	{
		Globals::instance().db()->changeUserInfo(m_user->username(), txtPassword->text(), txtEmail->text());
		
		QMessageBox::information(this, tr("Success"), tr("Profile updated"));
	}
	catch (Exception* ex)
	{
		ExceptionHandler::instance().protocolException(this->thread(), ex, TRUE);
	}
}

void UserPropertiesDialog::listCompleteDoubleClicked(QModelIndex )
{
	if (!listComplete->selectionModel()->hasSelection() || !listComplete->selectionModel()->selectedIndexes().first().isValid())
		return;

	try
	{
		ModelTorrent* model = Globals::instance().db()->createModelTorrent(listComplete->selectionModel()->selectedIndexes().first().data().toString());

		TorrentPropertiesDialog dlg(model, this);
		dlg.exec();

		delete model;
	}
	catch (Exception* ex)
	{
		ExceptionHandler::instance().protocolException(this->thread(), ex, TRUE);
		QMessageBox::warning(this, tr("QTracker"), tr("An uknown error occurred."));

		return;
	}
}

void UserPropertiesDialog::listIncompleteDoubleClicked(QModelIndex )
{
	if (!listIncomplete->selectionModel()->hasSelection() || !listIncomplete->selectionModel()->selectedIndexes().first().isValid())
		return;

	try
	{
		ModelTorrent* model = Globals::instance().db()->createModelTorrent(listIncomplete->selectionModel()->selectedIndexes().first().data().toString());

		TorrentPropertiesDialog dlg(model, this);
		dlg.exec();

		delete model;
	}
	catch (Exception* ex)
	{
		ExceptionHandler::instance().protocolException(this->thread(), ex, TRUE);
		QMessageBox::warning(this, tr("QTracker"), tr("An uknown error occurred."));

		return;
	}
}
